package com.kaibes.admin.site;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.kaibes.core.base.util.ObjectUtils;
import com.kaibes.module.value.KeyValueService;
import com.kaibes.web.admin.AdminMenu;
import com.kaibes.web.admin.AdminMenuRes;
import com.kaibes.web.thymeleaf.ThymeleafConfig;

@Configuration
public class SiteInit implements InitializingBean {
    
    @Autowired
    private ThymeleafConfig webConfig;
    @Autowired
    private KeyValueService mapService;
    @Autowired
    private SiteInfo siteInfo;
    
    @Bean
    public AdminMenu siteMenu() {
        AdminMenu menu = new AdminMenu();
        menu.setKey("sites");
        menu.setName("站点设置");
        menu.setIcon("fab fa-internet-explorer");
        menu.setHref("/admin/site/setting");
        menu.setSort(3);
        menu.setParentKey(AdminMenuRes.MENU_SETTING);
        return menu;
    }

	@Override
	public void afterPropertiesSet() throws Exception {
		SiteInfo temp = mapService.getObject(SiteInfo.class);
    	if (temp == null) {
			siteInfo.setTitle("kaibes");
			siteInfo.setDescription("又一个KAIBES网站");
			siteInfo.setKeywords("KAIBES");
			mapService.saveObject(siteInfo);
		} else {
			ObjectUtils.copyProperties(temp, siteInfo);
		}
    	webConfig.putStaticVariable("siteInfo", siteInfo);
	}
}
